using MicroMsg.Common.Utils;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.Xml.Linq;
namespace MicroMsg.Manager
{
	public class BlogConversationMgr
	{
		private const string TAG = "BlogConversationMgr";
		public const int NUM_PER_PAGE = 1;
		private static int int_0;
		public static void clearAllMsg()
		{
			ConversationMgr.delConversation(ConstantValue.TAG_BLOGAPP);
			StorageMgr.chatMsg.delByTalker(ConstantValue.TAG_BLOGAPP);
		}
		public static bool getGroupMsg(int startnum, ref string showTime, ref List<TXNews> appMsgList)
		{
			int msgNum = StorageMgr.chatMsg.getMsgNum(ConstantValue.TAG_BLOGAPP);
			if (msgNum <= 0)
			{
				return false;
			}
			List<ChatMsg> msgList;
			if (startnum - 1 < 0)
			{
				msgList = StorageMgr.chatMsg.getMsgList(ConstantValue.TAG_BLOGAPP, 0, startnum);
			}
			else
			{
				msgList = StorageMgr.chatMsg.getMsgList(ConstantValue.TAG_BLOGAPP, startnum - 1, 1);
			}
			if (msgList == null || msgList.get_Count() <= 0)
			{
				return false;
			}
			appMsgList = BlogConversationMgr.convertChatMsg2BlogMsgList(msgList.get_Item(0));
			if (appMsgList != null && appMsgList.get_Count() > 0)
			{
				showTime = Util.formatTimeInChatMsg(msgList.get_Item(0).nCreateTime);
				return true;
			}
			return false;
		}
		public static List<TXNews> convertChatMsg2BlogMsgList(ChatMsg chatMsg_0)
		{
			if (chatMsg_0 == null || chatMsg_0.strMsg == null)
			{
				return null;
			}
			List<TXNews> list = BlogConversationMgr.parseNewsXmlList(chatMsg_0.strMsg);
			if (list != null && list.get_Count() >= 0)
			{
				using (List<TXNews>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						TXNews current = enumerator.get_Current();
						current.nChatMsgLocalID = chatMsg_0.nMsgLocalID;
					}
				}
				return list;
			}
			return null;
		}
		public static List<TXNews> parseNewsXmlList(string strXml)
		{
			if (string.IsNullOrEmpty(strXml))
			{
				return null;
			}
			if (!strXml.StartsWith("<"))
			{
				Log.smethod_5("BlogConversationMgr", "strXml need cut");
				int num = strXml.IndexOf("<", 0);
				strXml = strXml.Substring(num);
			}
			if (string.IsNullOrEmpty(strXml))
			{
				return null;
			}
			List<TXNews> result;
			try
			{
				XDocument xDocument = XDocument.Parse(strXml);
				XElement xElement = xDocument.Element("mmreader");
				if (xElement == null)
				{
					result = null;
				}
				else
				{
					List<TXNews> list = new List<TXNews>();
					string type = "";
					XElement xElement2 = xElement.Element("category");
					XAttribute xAttribute = xElement2.Attribute("type");
					if (xAttribute != null)
					{
						type = xAttribute.get_Value();
					}
					IEnumerable<XElement> enumerable = xElement2.Elements("item");
					using (IEnumerator<XElement> enumerator = enumerable.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							XElement current = enumerator.get_Current();
							TXNews tXNews = new TXNews();
							if (current != null)
							{
								XElement xElement3 = current.Element("title");
								if (xElement3 != null)
								{
									tXNews.strTitle = xElement3.get_Value();
								}
								XElement xElement4 = current.Element("url");
								if (xElement4 != null)
								{
									tXNews.strUrl = xElement4.get_Value();
								}
								XElement xElement5 = current.Element("shorturl");
								if (xElement5 != null)
								{
									tXNews.strShorturl = xElement5.get_Value();
								}
								XElement xElement6 = current.Element("longurl");
								if (xElement6 != null)
								{
									tXNews.strLongurl = xElement6.get_Value();
								}
								XElement xElement7 = current.Element("pub_time");
								if (xElement7 != null)
								{
									tXNews.nPubTime = long.Parse(xElement7.get_Value());
								}
								XElement xElement8 = current.Element("cover");
								if (xElement8 != null)
								{
									int num2 = xElement8.get_Value().IndexOf(".jpg");
									if (num2 != -1)
									{
										tXNews.strCoverImgUrl = xElement8.get_Value().Substring(0, num2 + 4);
									}
								}
								XElement xElement9 = current.Element("tweetid");
								if (xElement9 != null)
								{
									tXNews.strTweetid = xElement9.get_Value();
								}
								XElement xElement10 = current.Element("sources");
								if (xElement10 != null)
								{
									XElement xElement11 = xElement10.Element("source");
									if (xElement11 != null)
									{
										XElement xElement12 = xElement11.Element("name");
										if (xElement12 != null)
										{
											tXNews.strSource = xElement12.get_Value();
										}
										XElement xElement13 = xElement11.Element("icon");
										if (xElement13 != null)
										{
											tXNews.strIconUrl = xElement13.get_Value();
										}
									}
								}
								tXNews.type = type;
								list.Add(tXNews);
							}
						}
					}
					result = list;
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("BlogConversationMgr", ex.ToString());
				result = null;
			}
			return result;
		}
		public static void testNewMsg()
		{
			for (int i = BlogConversationMgr.int_0; i < 10 + BlogConversationMgr.int_0; i++)
			{
				ChatMsg chatMsg = new ChatMsg();
				chatMsg.nMsgType = 1;
				chatMsg.nMsgSvrID = 0;
				chatMsg.strTalker = ConstantValue.TAG_BLOGAPP;
				chatMsg.strMsg = "<mmreader><category type=\"11\" count=\"1\"><name><![CDATA[微博推荐]]></name><topnew><cover><![CDATA[]]></cover><width><![CDATA[0]]></width><height><![CDATA[0]]></height></topnew><item><title><![CDATA[落单的朋友" + i + "]]></title><url><![CDATA[http://yd.weixin.qq.com/reader/11/0vJClS?m=1103743700&t=1337070230&r=622006290&n=1]]></url><shorturl><![CDATA[http://url.cn/0vJClS]]></shorturl><longurl><![CDATA[http://www.ftchinese.com/users/gotourl?ccode=2G120008&e=b&pageurl=aHR0cDovL3d3dy5mdGNoaW5lc2UuY29tL3N0b3J5LzAwMTA0NDUxOA%3D%3D&via=170050_WB.3RDAPI.URL.0.0]]></longurl><pub_time>1337054831</pub_time><cover><![CDATA[]]></cover><tweetid>27848069689104</tweetid><sources><source><name><![CDATA[FT中文网]]></name><icon><![CDATA[http://t1.qlogo.cn/mbloghead/0264cdb77c95e10da0e4/120]]></icon></source></sources></item></category></mmreader>";
				StorageMgr.chatMsg.addMsg(chatMsg);
			}
			BlogConversationMgr.int_0 += 10;
		}
	}
}
